import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/entity/getcash_record_entity.dart';
import 'package:taonile/utils/string_utils.dart';
import 'package:taonile/views/default_views.dart';

import '../entity/asset_general_detail_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../views/dialog_manager.dart';

///
/// 实体店消费记录
///
class StorePayRecord extends StatefulWidget {
  const StorePayRecord({Key? key}) : super(key: key);

  @override
  State<StorePayRecord> createState() => _StorePayRecordState();
}

class _StorePayRecordState extends State<StorePayRecord> {
  var page = 1;
  List<GetcashRecordDataData> datas = [];

  @override
  void initState() {
    super.initState();
    getDatas();
  }

  getDatas() async {
    dynamic result = await Api.getCashRecord({'page': page, 'limit': '20'});
    GetcashRecordEntity entity = GetcashRecordEntity.fromJson(result);
    setState(() {
      if (page == 1) {
        datas.clear();
      }
      if (entity.data.data.isNotEmpty) {
        page++;
        datas.addAll(entity.data.data);
      }
    });
  }

  Widget getListItem(int index) {
    return Container(
      // height: 124.w,
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      decoration: BoxDecoration(
        color: (Config.colorWhite),
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '',
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 15.w),
              ),
              Text(
                //datas[index].accountText.realName
                StringUtils.getNameHide(datas[index].accountText.realName),
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 15.w),
              ),
              Text(
                datas[index].createdAt,
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText3,
                ),
              ),
            ],
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                '金券：${datas[index].money}',
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 60.w),
              ),
              Text(
                '',
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Visibility(
                visible: datas.isNotEmpty && datas[index].reason.isNotEmpty,
                child: Padding(
                  padding: EdgeInsets.only(top: 5.w),
                ),
              ),
              Visibility(
                visible: datas.isNotEmpty && datas[index].reason.isNotEmpty,
                child: Text(
                  datas[index].reason,
                  style: TextStyle(
                    fontSize: 22.w,
                    color: Config.colorText1,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: Text('消费记录'),
          centerTitle: true,
        ),
        body: Container(
            width: double.infinity,
            height: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
            margin: EdgeInsets.only(left: 25.w, right: 25.w, bottom: 5.w, top: 115.w),
            child: datas.isNotEmpty
                ? EasyRefresh(
                    header: const MaterialHeader(),
                    footer: const MaterialFooter(),
                    onRefresh: () async {
                      await Future.delayed(const Duration(milliseconds: 200), () {
                        page = 1;
                        getDatas();
                      });
                    },
                    onLoad: () async {
                      getDatas();
                    },
                    child: ListView.builder(
                      // physics: const NeverScrollableScrollPhysics(),
                      itemCount: datas.length,
                      padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 1.w),
                      itemBuilder: (BuildContext context, int index) {
                        return getListItem(index);
                      },
                    ),
                  )
                : showNullData()));
  }
}
